/**
 * @BelongsProject : LeetCode
 * @BelongsPackage : PACKAGE_NAME
 * @Author : WXY
 * @CreateTime : 2022-12-04  20:56
 * @Description : 704. 二分查找
 * @Version : 1.0
 */

public class Num704_search {
    public static int search(int[] nums, int target) {
        int L = 0;
        int R = nums.length - 1;
        int mid = 0;
        while (L <= R) {
            mid = (L + R ) / 2;
            if (nums[mid] < target) {
                L = mid + 1;
            } else if (nums[mid] > target) {
                R = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int search = search(arr, 4);
        System.out.println(search);

    }
}
